<?php
/**
*  +----------------------------------------------------------------------------------------------+
*   | Explain:
*  +----------------------------------------------------------------------------------------------+
*   | Author: ONLY <491518132@qq.com>
*	+----------------------------------------------------------------------------------------------+
*   | Creater Time : 2013-12-30 	
*  +----------------------------------------------------------------------------------------------+
*   | Link :		http://www.phpyrb.com	     
*  +----------------------------------------------------------------------------------------------+
**/

	class QuestionAction extends BaseAction {
		protected $model;
		function _initialize(){
			parent::_initialize();
			$this->model = M('Faqs');
			$status = array('未回答','已回答');
			$this->assign('status',$status);
		}
		
		/**
		 * (non-PHPdoc)
		 * @see BaseAction::index()
		 * 问题列表
		 */
		function index(){
			$condtion = array();
			$count = $this->usertoques(array('type' => 0));
			$list = array();
			if($count > 0){
				$page = new Page($count,35);
				$limit = $page->firstRow . ',' . $page->listRows ;
				$list = $this->usertoques(array('type' => 0),$limit);
			}
// 			dump($list);
			$this->assign('list',$list);
			$this->display();
		}
		
		/**
		 * 问题解答
		 */
		function detail(){
			$qid = $_REQUEST['id'];
			$info = $this->answer($qid);
// 			dump($info);
			$squest = array_shift($info);
			$this->assign('sq',$squest);
			$this->assign('answer',$info);
			$this->display();
		}
		     
		/**
		 * 问题提交
		 */
		function subsquet(){
			$data = array();
			$data['content'] = $_REQUEST['content'];
			$data['status'] =  '0';
			$data['type'] = '1';
			$data['ctime'] = time();
			$data['sire_id'] = $_REQUEST['id'];
			$data['add_ip'] = get_client_ip();
			$data['platform'] = '';
			$data['user_id'] = $this->userid;
			$reid = $this->add_updata($data);
			if($reid){
				$reid = $this->add_updata(array('status'=>1),array('id'=>$_REQUEST['id']),false);
				if($reid){
					$this->_ajm(200,'解答成功！');
				}else{
					$this->_ajm(300,'操作失败！',1);
				}
			}else{
				$this->_ajm(300,'操作失败！',1);
			}
		}
		
		/**
		 * 问题表单
		 */
		function edit(){
			$id = $_GET['id'];
			$info = $this->answer($id,FALSE);
// 			dump($id);
// 			dump($info);
			$this->assign('question',array_shift($info));
			$this->assign('info',$info);
			$this->display();
		}
		
		/**
		 * 返回用户的问题列表
		 * @param int $_userid
		 * @param int $_type
		 * @param int $_status
		 * @return array $info
		 */
		function usertoques($_condition = array(),$_limit = 'all'){
			if($_limit == 'all'){
				$info = $this->model->where($_condition)->count();
			}else{
				$info = $this->model->where($_condition)->limit($_limit)->order('id DESC')->select();
			}
			return $info;
		}
		
		/**
		 * 问题回答
		 * @param int $_quseid
		 * @return array $info
		 */
		function answer($_quseid,$_status = 'no'){
			$status = $_status ? ($_status == 'no' ? ' AND status != 3' : ' AND status = '.$_status) : '';
			$sql = 'SELECT * FROM `faqs` WHERE (( `id` = '.$_quseid.' ) OR ( `sire_id` ='. $_quseid.' ))'.$status;
			$info = $this->model->query($sql);
// 			echo $this->model->getLastSql();
			return $info;
		}
		
		/**
		 * 问题添加、修改
		 * @param array $_data 
		 * @param array $_where 条件
		 * @param string $_type true 为添加一条数据，false 为更新
		 * @return boolean
		 */
		function add_updata($_data = array(),$_where = array(),$_type = true){
			$reid = 1;
			if($_type){
				$reid = $this->model->add($_data);
// 				dump($this->model->getLastSql());
// 				exit;
			}else{
				$reid = $this->model->where($_where)->save($_data);
			}
			if($reid === FALSE){
				return 0;
			}else{
				return 1;
			}
		}
	}